﻿Imports System.Data.OleDb
Public Class frmThemQuyCach
#Region "Form Event"
    Private QuyCachDongGoiId As Integer = -1
    Private Sub frmThemQuyCach_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            lblEmpty.Visible = False
            txtTenQuyCachDongGoi.Text = ""
            QuyCachDongGoi_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Button Event"
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            If txtTenQuyCachDongGoi.Text = "" Then
                lblEmpty.Visible = True
                EmptyData(txtTenQuyCachDongGoi)
                Exit Sub
            Else
                lblEmpty.Visible = False
            End If
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Dim dbCommand As OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            Dim query As String = ""
            If QuyCachDongGoiId = -1 Then
                query = "Insert Into QuyCachDongGoi(TenQuyCachDongGoi) Values(N'" & txtTenQuyCachDongGoi.Text & "')"
            Else
                query = "Update QuyCachDongGoi Set TenQuyCachDongGoi=N'" & txtTenQuyCachDongGoi.Text & "' Where QuyCachDongGoiId=" & QuyCachDongGoiId
            End If
            dbCommand = New OleDbCommand(query, dbConn)
            dbCommand.ExecuteNonQuery()
            dbConn.Close()
            txtTenQuyCachDongGoi.Text = ""
            QuyCachDongGoiId = -1
            QuyCachDongGoi_Load()
            'frmThemQuyCach.DonViTinh_Load()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Try
            txtTenQuyCachDongGoi.Text = ""
            QuyCachDongGoiId = -1
            Me.Close()
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        Try
            QuyCachDongGoiId = dgvQuyCachDongGoi.Rows(dgvQuyCachDongGoi.CurrentCell.RowIndex).Cells("IdQuyCachDongGoi").Value
            txtTenQuyCachDongGoi.Text = dgvQuyCachDongGoi.Rows(dgvQuyCachDongGoi.CurrentCell.RowIndex).Cells("TenQuyCachDongGoi").Value
        Catch ex As Exception
            ShowError()
        End Try
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Try
            txtTenQuyCachDongGoi.Text = ""
            QuyCachDongGoiId = -1
            Dim IdDonVi As Integer = dgvQuyCachDongGoi.Rows(dgvQuyCachDongGoi.CurrentCell.RowIndex).Cells("IdQuyCachDongGoi").Value
            Dim TenDonVi As String = dgvQuyCachDongGoi.Rows(dgvQuyCachDongGoi.CurrentCell.RowIndex).Cells("TenQuyCachDongGoi").Value
            Dim dlgRes As DialogResult
            dlgRes = MessageBox.Show( _
                  "Bạn muốn xóa  : " & TenDonVi, _
                  "Xóa Đơn Vị Tính", _
            MessageBoxButtons.OKCancel, _
                  MessageBoxIcon.Question)
            If dlgRes = DialogResult.OK Then
                Dim connectAccess As New DataAccess
                Dim dbConn As New OleDb.OleDbConnection
                Dim dbCommand As OleDbCommand
                Dim query As String = "Update QuyCachDongGoi Set Xoa=1 Where QuyCachDongGoiID=" & IdDonVi
                connectAccess.ConnectDatabase_DBAccess(dbConn)
                dbCommand = New OleDbCommand(query, dbConn)
                dbCommand.ExecuteNonQuery()
                dbConn.Close()
                QuyCachDongGoi_Load()
            End If
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region
#Region "Load Data"
    Private Sub QuyCachDongGoi_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dtQuyCachDongGoi As New DataTable
            Dim query As String = "Select * From QuyCachDongGoi Where Xoa=0 Order By QuyCachDongGoiID ASC"
            Dim adapter As OleDb.OleDbDataAdapter
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            adapter = New OleDb.OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtQuyCachDongGoi)
            dbConn.Close()
            dtQuyCachDongGoi.Columns.Add("STT")
            For i As Integer = 0 To dtQuyCachDongGoi.Rows.Count - 1
                dtQuyCachDongGoi.Rows(i)("STT") = i + 1
            Next
            dgvQuyCachDongGoi.AutoGenerateColumns = False
            dgvQuyCachDongGoi.DataSource = dtQuyCachDongGoi
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
#End Region

    Private Sub txtTenDonVi_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTenQuyCachDongGoi.KeyUp
        Try
            txtTenQuyCachDongGoi.BackColor = Color.White
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MenuChinhSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuChinhSua.Click

    End Sub
End Class